package com.castle.fortress.admin.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.system.entity.SysCountryEntity;
import com.castle.fortress.admin.system.dto.SysCountryDto;
import com.castle.fortress.admin.system.service.SysCountryService;
import com.castle.fortress.common.utils.ConvertUtil;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.enums.OperationTypeEnum;
import com.castle.fortress.admin.core.annotation.CastleLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;
import org.apache.logging.log4j.util.Strings;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.castle.fortress.admin.utils.ExcelUtils;
import javax.servlet.http.HttpServletResponse;
import com.castle.fortress.common.entity.DynamicExcelEntity;
import java.util.List;

/**
 * 全球国家地区信息 控制器
 *
 * @author mjj
 * @since 2023-07-01
 */
@Api(tags="全球国家地区信息管理控制器")
@Controller
public class SysCountryController {
    @Autowired
    private SysCountryService sysCountryService;

    /**
     * 全球国家地区信息的分页展示
     * @param sysCountryDto 全球国家地区信息实体类
     * @param current 当前页
     * @param size  每页记录数
     * @return
     */
    @CastleLog(operLocation="全球国家地区信息",operType = OperationTypeEnum.QUERY)
    @ApiOperation("全球国家地区信息分页展示")
    @GetMapping("/system/sysCountry/page")
    @ResponseBody
    @RequiresPermissions("system:sysCountry:pageList")
    public RespBody<IPage<SysCountryDto>> pageSysCountry(SysCountryDto sysCountryDto, @RequestParam(required = false) Integer current, @RequestParam(required = false)Integer size){
        Integer pageIndex= current==null? GlobalConstants.DEFAULT_PAGE_INDEX:current;
        Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
        Page<SysCountryDto> page = new Page(pageIndex, pageSize);
        IPage<SysCountryDto> pages = sysCountryService.pageSysCountry(page, sysCountryDto);

        return RespBody.data(pages);
    }

    /**
     * 全球国家地区信息的列表展示
     * @param sysCountryDto 全球国家地区信息实体类
     * @return
     */
    @CastleLog(operLocation="全球国家地区信息",operType = OperationTypeEnum.QUERY)
    @ApiOperation("全球国家地区信息列表展示")
    @GetMapping("/system/sysCountry/list")
    @ResponseBody
    public RespBody<List<SysCountryDto>> listSysCountry(SysCountryDto sysCountryDto){
        List<SysCountryDto> list = sysCountryService.listSysCountry(sysCountryDto);
        return RespBody.data(list);
    }

    /**
     * 全球国家地区信息保存
     * @param sysCountryDto 全球国家地区信息实体类
     * @return
     */
    @CastleLog(operLocation="全球国家地区信息",operType = OperationTypeEnum.INSERT)
    @ApiOperation("全球国家地区信息保存")
    @PostMapping("/system/sysCountry/save")
    @ResponseBody
    @RequiresPermissions("system:sysCountry:save")
    public RespBody<String> saveSysCountry(@RequestBody SysCountryDto sysCountryDto){
        if(sysCountryDto == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        SysCountryEntity sysCountryEntity = ConvertUtil.transformObj(sysCountryDto,SysCountryEntity.class);
        if(sysCountryService.save(sysCountryEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 全球国家地区信息编辑
     * @param sysCountryDto 全球国家地区信息实体类
     * @return
     */
    @CastleLog(operLocation="全球国家地区信息",operType = OperationTypeEnum.UPDATE)
    @ApiOperation("全球国家地区信息编辑")
    @PostMapping("/system/sysCountry/edit")
    @ResponseBody
    @RequiresPermissions("system:sysCountry:edit")
    public RespBody<String> updateSysCountry(@RequestBody SysCountryDto sysCountryDto){
        if(sysCountryDto == null || sysCountryDto.getId() == null || sysCountryDto.getId().equals(0L)){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        SysCountryEntity sysCountryEntity = ConvertUtil.transformObj(sysCountryDto,SysCountryEntity.class);
        if(sysCountryService.updateById(sysCountryEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 全球国家地区信息删除
     * @param id
     * @return
     */
    @CastleLog(operLocation="全球国家地区信息",operType = OperationTypeEnum.DELETE)
    @ApiOperation("全球国家地区信息删除")
    @PostMapping("/system/sysCountry/delete")
    @ResponseBody
    @RequiresPermissions("system:sysCountry:delete")
    public RespBody<String> deleteSysCountry(@RequestParam Long id){
        if(id == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(sysCountryService.removeById(id)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }


    /**
     * 全球国家地区信息批量删除
     * @param ids
     * @return
     */
    @CastleLog(operLocation="全球国家地区信息",operType = OperationTypeEnum.DELETE)
    @ApiOperation("全球国家地区信息批量删除")
    @PostMapping("/system/sysCountry/deleteBatch")
    @ResponseBody
    @RequiresPermissions("system:sysCountry:deleteBatch")
    public RespBody<String> deleteSysCountryBatch(@RequestBody List<Long> ids){
        if(ids == null || ids.size()<1){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(sysCountryService.removeByIds(ids)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 全球国家地区信息详情
     * @param id 全球国家地区信息id
     * @return
     */
    @CastleLog(operLocation="全球国家地区信息",operType = OperationTypeEnum.QUERY)
    @ApiOperation("全球国家地区信息详情")
    @GetMapping("/system/sysCountry/info")
    @ResponseBody
    @RequiresPermissions("system:sysCountry:info")
    public RespBody<SysCountryDto> infoSysCountry(@RequestParam Long id){
        if(id == null){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        SysCountryEntity sysCountryEntity = sysCountryService.getById(id);
        SysCountryDto sysCountryDto = ConvertUtil.transformObj(sysCountryEntity,SysCountryDto.class);

        return RespBody.data(sysCountryDto);
    }

	/**
     * 动态表头导出 依据展示的字段导出对应报表
     * @param dynamicExcelEntity
     * @param response
     * @throws Exception
     */
    @CastleLog(operLocation="全球国家地区信息",operType = OperationTypeEnum.EXPORT)
	@PostMapping("/system/sysCountry/exportDynamic")
	@ApiOperation("动态表头导出，依据展示的字段导出对应报表")
	public void exportDynamic(@RequestBody DynamicExcelEntity<SysCountryDto> dynamicExcelEntity, HttpServletResponse response) throws Exception {
		List<SysCountryDto> list = sysCountryService.listSysCountry(dynamicExcelEntity.getDto());
		//字典、枚举、接口、json常量等转义后的列表数据 根据实际情况初始化该对象，null为list数据直接导出
		List<List<Object>> dataList = null;
		/**
        * 根据实际情况初始化dataList,可参照 com.castle.fortress.admin.system.controller.TmpDemoController类中方法：exportDynamic
        */
		ExcelUtils.exportDynamic(response,dynamicExcelEntity.getFileName()+".xlsx",null,list,dynamicExcelEntity.getHeaderList(),dataList);
	}


}
